"""构建配置管理"""
import os
from dataclasses import dataclass
from typing import Optional

@dataclass
class BuildConfig:
    """引擎构建配置"""
    onnx_path: str
    engine_path: str
    ppq_json_path: Optional[str] = None
    workspace_size: int = 1 << 30
    enable_fp16: bool = False
    enable_int8: bool = True
    
    def __post_init__(self):
        """验证配置"""
        if not os.path.exists(self.onnx_path):
            raise FileNotFoundError(f"ONNX file not found: {self.onnx_path}")
        
        if self.enable_int8 and not self.ppq_json_path:
            raise ValueError("PPQ JSON path is required for INT8 quantization")
        
        if self.ppq_json_path and not os.path.exists(self.ppq_json_path):
            raise FileNotFoundError(f"PPQ JSON file not found: {self.ppq_json_path}")
        
        # 确保输出目录存在
        os.makedirs(os.path.dirname(self.engine_path), exist_ok=True)

def create_default_config(models_dir: str) -> BuildConfig:
    """创建默认配置"""
    return BuildConfig(
        onnx_path=os.path.join(models_dir, 'onnx/weights/yolov11n_int8.onnx'),
        ppq_json_path=os.path.join(models_dir, 'onnx/weights/yolov11n_int8.json'),
        engine_path=os.path.join(models_dir, 'engine/yolov11n_int8.engine')
    )